#!/usr/bin/env python
# -*- coding:utf-8 -*-

import unittest
from selenium import webdriver
from bs4 import BeautifulSoup as bs


class douyu(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.PhantomJS()
        self.num = 0

    def testDouyu(self):
        self.driver.get("https://www.douyu.com/directory/all")
        while True:
            soup = bs(self.driver.page_source, "lxml")
            # 房间，返回列表
            name_list = soup.find_all("h3", {"class": "ellipsis"})
            # 人数
            number_list = soup.find_all("span", {"class": "dy-num fr"})
            # zip
            for name, number in zip(name_list, number_list):
                print u"人数:" + number.get_text().strip() + u"\t房间名:" + name.get_text().strip()
                self.num += 1

            if self.driver.page_source.find("shark-pager-disable-next") != -1:
                # 结束
                break

            self.driver.find_element_by_class_name("shark-pager-next").click()


    def tearDown(self):
        print "当前网站直播人数" + str(self.num)
        self.driver.quit()


if __name__ == "__main__":
    unittest.main()
